<template>
  <el-main>
    <!-- 搜索 -->
    <el-form
      :model="searchParm"
      label-width="80px"
      :inline="true"
      size="default"
    >
      <el-form-item>
        <el-input
          v-model="searchParm.floorName"
          placeholder="请输入楼层名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" @click="searchBtn">搜索</el-button>
        <el-button icon="Close" type="danger" plain @click="resetBtn"
          >重置</el-button
        >
        <el-button icon="Plus" type="primary" @click="addBtn">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :height="tableHeight" :data="tableList" border stripe>
      <el-table-column prop="floorName" label="楼层名称"></el-table-column>
      <el-table-column prop="floorNum" label="楼层层数"></el-table-column>
      <el-table-column prop="orderNum" label="序号"></el-table-column>
      <el-table-column label="操作" align="center" width="220">
        <template #default="scope">
          <el-button
            type="primary"
            icon="Edit"
            size="default"
            @click="editBtn(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            icon="Delete"
            size="default"
            @click="deleteBtn(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page.sync="searchParm.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="searchParm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="searchParm.total"
      background
    >
    </el-pagination>
  </el-main>
  <!-- 新增弹框 -->
  <SysDialog
    :title="dialog.title"
    :width="dialog.width"
    :visible="dialog.visible"
    :height="dialog.height"
    @on-close="onClose"
    @on-confirm="commit"
  >
    <template v-slot:content>
      <el-form
        :model="addModel"
        ref="addRef"
        :rules="rules"
        label-width="80px"
        :inline="false"
        size="default"
      >
        <el-form-item prop="floorName" label="楼层名称">
          <el-input
            v-model="addModel.floorName"
            placeholder="请输入楼层名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="floorNum" label="楼层层数">
          <el-input
            v-model="addModel.floorNum"
            placeholder="请输入楼层层数"
          ></el-input>
        </el-form-item>
        <el-form-item prop="orderNum" label="序号">
          <el-input
            v-model="addModel.orderNum"
            placeholder="请输入楼层序号"
          ></el-input>
        </el-form-item>
      </el-form>
    </template>
  </SysDialog>
</template>

<script setup lang="ts">
import { nextTick, onMounted, reactive, ref } from "vue";
import SysDialog from "@/components/SysDialog.vue";
import useDialog from "@/hooks/useDialog";
import { ElMessage, FormInstance } from "element-plus";
import {
  addApi,
  getListApi,
  RoomFloor,
  editApi,
  deleteApi,
} from "@/api/room_floor/index";
import useInstance from "@/hooks/useInstance";
const { global } = useInstance();
//表单ref属性
const addRef = ref<FormInstance>();
//获取弹框属性
const { dialog, onClose, onShow } = useDialog();
//搜索表单绑定的对象
const searchParm = reactive({
  currentPage: 1,
  pageSize: 10,
  floorName: "",
  total: 0,
});
//新增表单绑定的对象
const addModel = reactive({
  floorId: "",
  floorName: "",
  floorNum: "",
  orderNum: "",
});
//表单验证规则
const rules = reactive({
  floorName: [
    {
      required: true,
      trigger: ["blur", "change"],
      message: "请填写楼层名称",
    },
  ],
  floorNum: [
    {
      required: true,
      trigger: ["blur", "change"],
      message: "请填写楼层层数",
    },
  ],
});
//标识:区分新增和编辑
const tags = ref("");
//新增按钮
const addBtn = () => {
  tags.value = "0";
  dialog.title = "新增";
  dialog.height = 180;
  onShow();
  //表单清空
  addRef.value?.resetFields();
};
//编辑
const editBtn = (row: RoomFloor) => {
  tags.value = "1";
  dialog.title = "编辑";
  dialog.height = 180;
  onShow();
  //回显数据
  nextTick(() => {
    Object.assign(addModel, row);
  });
  //表单清空
  addRef.value?.resetFields();
};
//删除
const deleteBtn = async (row: RoomFloor) => {
  console.log(row);
  const confirm = await global.$myconfirm("确定删除该数据吗?");
  if (confirm) {
    let res = await deleteApi(row.floorId);
    if (res && res.code == 200) {
      ElMessage.success(res.msg);
      //刷新表格
      getList();
    }
  }
};
//搜索
const searchBtn = () => {
  getList();
};
//重置
const resetBtn = () => {
  searchParm.currentPage = 1;
  searchParm.floorName = "";
  getList();
};

//表单提交
const commit = () => {
  //表单验证
  addRef.value?.validate(async (valid) => {
    if (valid) {
      let res = null;
      if (tags.value == "0") {
        res = await addApi(addModel);
      } else {
        res = await editApi(addModel);
      }
      if (res && res.code == 200) {
        ElMessage.success(res.msg);
        //刷新表格
        getList();
        onClose();
      }
    }
  });
};
//表格数据
const tableList = ref([]);
//获取表格数据
const getList = async () => {
  let res = await getListApi(searchParm);
  if (res && res.code == 200) {
    console.log(res);
    tableList.value = res.data.records;
    searchParm.total = res.data.total;
  }
};
//页容量改变时触发
const sizeChange = (size: number) => {
  searchParm.pageSize = size;
  getList();
};
//页数改变时触发
const currentChange = (page: number) => {
  searchParm.currentPage = page;
  getList();
};
//表格高度
const tableHeight = ref(0);
onMounted(() => {
  getList();
  nextTick(() => {
    tableHeight.value = window.innerHeight - 230;
  });
});
</script>

<style scoped></style>
